﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using NP.BPMReportPlatform.CustomerProfileConfig;
using NP.BPMReportPlatform.CustomerProfileConfig.Dto;
using System.Collections.Generic;
using NP.BPMReportPlatform.Entities.BPM_Business;
using System.Runtime.InteropServices;

namespace NP.BPMReportPlatform.SystemSettings
{
    /// <summary>
    /// 客户档案维护接口
    /// </summary>
    [ApiController]
    [Route("api/CustomerProfileConfig")]
    public class CustomerProfileConfigController : BPMReportPlatformController
    {
        private readonly ICustomerProfileConfigAppService _appService;

        public CustomerProfileConfigController(ICustomerProfileConfigAppService appService)
        {
            _appService = appService;
        }

        /// <summary>
        /// 根据送达方编码找售达方编码
        /// </summary>
        /// <param name="business">事业部</param>
        /// <param name="code">送达方编码</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetSDFInfo")]
        public async Task<List<SDFInfoOutput>> GetSDFInfo([FromQuery] string business, [FromQuery] string code)
        {
            return await _appService.GetSDFInfo(business, code);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetAllAsync")]
        public async Task<PagedResultDto<GetCustomerProfileConfigOutput>> GetAllAsync([FromBody] GetCustomerProfileConfigInput input)
        {
            return await _appService.GetAllAsync(input);
        }
        /// <summary>
        /// 查询检测指标
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetIndicatorsList")]
        public async Task<List<CustomerProfileConfigDetail>> GetIndicatorsList([FromQuery] Guid id)
        {
            return await _appService.GetIndicatorsList(id);
        }

        /// <summary>
        /// 查询修改记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetEditRecordList")]
        public async Task<List<EditRecordListOutput>> GetEditRecordList([FromQuery] Guid id)
        {
            return await _appService.GetEditRecordList(id);
        }

        /// <summary>
        /// 查询修改记录详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="version"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetEditRecordDetail")]
        public async Task<List<EditRecordDetailOutput>> GetEditRecordDetail([FromQuery] Guid id, [FromQuery] int version)
        {
            return await _appService.GetEditRecordDetail(id, version);
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportAsync")]
        public async Task<XlsxFileResult<GetCustomerProfileConfigOutput>> ExportAsync([FromBody] GetCustomerProfileConfigInput input)
        {
            var rst = await _appService.ExportAsync(input);
            return new XlsxFileResult<GetCustomerProfileConfigOutput>(rst, "导出Excel");
        }

        /// <summary>
        /// 保存审批配置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SaveAsync")]
        public async Task SaveAsync([FromBody] SaveCustomerProfileConfigInput input)
        {
            await _appService.SaveAsync(input);
        }

        /// <summary>
        /// 删除审批配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("DeleteAsync")]
        public async Task DeleteAsync([FromQuery] Guid id)
        {
            await _appService.DeleteAsync(id);
        }

        
        /// <summary>
        /// 批量导入Excel
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ImporterExcelAsync")]
        public async Task<ImportResultExt<ImportCustomerProfileConfigTemplate>> ImporterExcelAsync(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<ImportCustomerProfileConfigTemplate>();
                res.Exception = new Exception("请上传文件！");
                return res;
            }
            var s = file.OpenReadStream();
            var bytes = StreamToBytes(s);
            s.Close();
            return await _appService.ImporterExcel(bytes);
        }
        private byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
    }
}
